package components;

import java.security.InvalidParameterException;
import java.util.Calendar;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.util.Log;

import common.Constant;

public class PasscodeManager {
	public static final String TAG = PasscodeManager.class.getSimpleName();
	private static final String SHARED_PREFERENCE_KEY_PASSCODE = "passcode";
	public String mPasscode = null;
	public static PasscodeManager mPasscodeManager = null;
	public Date mLastTimeAcceptPasscode = null;
	public static int TIME_REQUIRED_PASSCODE = 5 * 60 * 1000; // miliseconds
	
	private PasscodeManager() {
		
	}
	
	public String getPassCode() {
		return mPasscode;
	}
	
	public static PasscodeManager sharedPasscode(Context context) {
		
		if (mPasscodeManager == null) {
			mPasscodeManager = loadPasscode(context);
		}
		
		return mPasscodeManager;
	}

	private static PasscodeManager loadPasscode(Context context) {
		// context.getApplicationInfo();
		// TODO Auto-generated method stub
		// boolean isDebuggable = (0 != (context.getApplicationInfo().flags &=
		// ApplicationInfo.FLAG_DEBUGGABLE));

		String passcode = null;

		// TODO: Load Phone Number
		SharedPreferences spref = context.getSharedPreferences(
				Constant.KEY_SHARED_PREFERENCE_NAME, Context.MODE_PRIVATE);
		passcode = spref.getString(SHARED_PREFERENCE_KEY_PASSCODE, null);

		mPasscodeManager = new PasscodeManager();
		mPasscodeManager.mPasscode = passcode;
		return mPasscodeManager;
	}

	/**
	 * 
	 * @param newpasscode
	 * @exception InvalidParameterException
	 * @exception NumberFormatException
	 */
	public void setPasscode(Context context, String newpasscode) {
		if (!validatePasscode(newpasscode)) {
			throw new NumberFormatException("Phone number is invalid");
		}

		if (context == null) {
			throw new InvalidParameterException("Context is null");
		}

		SharedPreferences spref = context.getSharedPreferences(
				Constant.KEY_SHARED_PREFERENCE_NAME, Context.MODE_PRIVATE);
		Editor edit = spref.edit();
		edit.putString(SHARED_PREFERENCE_KEY_PASSCODE, newpasscode);
		edit.commit();
		
		mPasscode = newpasscode;
	}

	public boolean validatePasscode(String passcode) {
		String passCodePattern = "^[0-9.()-]{4,4}$";
		Pattern pattern = Pattern.compile(passCodePattern);
		Matcher matcher = pattern.matcher(passcode);

		boolean result = matcher.matches();
		if (result) {
			String match = matcher.group();
			Log.d(TAG, "Passcode valid " + match);
		}

		return result;
	}
	
	public void clearPasscode(Context context) {
		String passcode = null;

		// TODO: Load Phone Number
		SharedPreferences spref = context.getSharedPreferences(
				Constant.KEY_SHARED_PREFERENCE_NAME, Context.MODE_PRIVATE);
		Editor editor = spref.edit();
		editor.remove(SHARED_PREFERENCE_KEY_PASSCODE);
		editor.commit();
		
		mPasscode = null;
	}
	
	public boolean isExpiredSession() {
		boolean isExpireSession = false;
		if (mPasscode != null && !mPasscode.isEmpty()) {
			
		if (mLastTimeAcceptPasscode == null) {
			isExpireSession = true;
		} else {
			Date now = Calendar.getInstance().getTime();

			long timeSpan = now.getTime() - mLastTimeAcceptPasscode.getTime();

			if (timeSpan > TIME_REQUIRED_PASSCODE) {
				isExpireSession = true;
			}
		}
		}
		
		return isExpireSession;
	}
	
		public boolean login(String passcode) {
			if (mPasscode == null) {
				return true;
			} else {
				boolean isMatch = checkPasscode(passcode);
				if (isMatch) {
					mLastTimeAcceptPasscode = Calendar.getInstance().getTime();
					
				}
				
				return isMatch;
			}
		}
		
		public boolean checkPasscode(String passcode) {
			if (mPasscode == null) {
				return true;
			} else if (passcode == null) {
				return false;
			} else if (passcode.equals(mPasscode)) {
				return true;
			} else {
				return false;
			}
		}
}
